package com.cabrida.livestar.main;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.model.FansAdvanceListData;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 全部预告
 */
public class FansAdvanceListActivity extends BaseActivity {
	public static final String TAG = FansAdvanceListActivity.class
			.getSimpleName();
	private ImageView iv_back;
	private TextView tv_title;
	private ListView listView_liveadvance;
	private FansHomePageAdvanceAdapter adapter;
	private ArrayList<FansAdvanceListData> infoList = new ArrayList<FansAdvanceListData>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
	};

	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(MainActivity.ACTION_ADVANCE_REFRESH)) {
				String follow = intent.getStringExtra("follow");
				getAdvancedata(follow);
			}
		}
	};

	/** 获取预播改变订阅状态 */
	private void getAdvancedata(final String follow) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("follow", follow);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_BOOK_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getAdvancedata", response.toString());

						int code = JsonUtils.getInt(response, "code", -1);
						//
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
							setAdvanceBook(follow);
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	/**
	 * 预播订阅状态修改
	 */
	private void setAdvanceBook(String follow) {
		ArrayList<FansAdvanceListData> data = adapter.getData();
		for (int i = 0; i < data.size(); i++) {
			if (follow.equals(data.get(i).getL_id())) {
				int ischeck = data.get(i).getIsCheck();
				if (ischeck == 0) {
					ischeck = 1;
				} else {
					ischeck = 0;
				}
				data.get(i).setIsCheck(ischeck);
			}
		}
		adapter.setData(data);
		adapter.notifyDataSetChanged();
	}

	/** 获取预告列表数据 */
	private void getAdvanceList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("page", 1);
		params.put("amount", 20);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_ADVANCE_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getAdvanceList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							JSONArray list = JsonUtils.getJSONArray(data,
									"list", new JSONArray());
							infoList = JSONParser.getFansAdvanceList(list);
							adapter.setData(infoList);
							adapter.notifyDataSetChanged();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,
									"data", new JSONObject());
							String message = JsonUtils.getString(data,
									"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_fans_advancelist);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		listView_liveadvance = (ListView) findViewById(R.id.listView_liveadvance);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		tv_title.setText(getString(R.string.fans_homepage_liveadvance_more));
		adapter = new FansHomePageAdvanceAdapter(context);
		listView_liveadvance.setAdapter(adapter);

		getAdvanceList();

		IntentFilter filter_ad = new IntentFilter();
		filter_ad.addAction(MainActivity.ACTION_ADVANCE_REFRESH);
		registerReceiver(mReceiver, filter_ad);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		default:
			break;
		}
	}

}
